/**
 * 
 */
package nl.samenloop.bigscreen;

import java.awt.Color;
import java.awt.Container;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import nl.samenloop.bigscreen.clock.CountdownClock;
import nl.samenloop.bigscreen.fx.FadeIn;
import nl.samenloop.bigscreen.fx.FadeOut;
import nl.samenloop.bigscreen.fx.FxListener;
import nl.samenloop.bigscreen.panels.CenterPanel;
import nl.samenloop.bigscreen.sequence.DrawingTarget;

/**
 * @author Bram Wiekens
 * 
 */
@SuppressWarnings("serial")
public class BigScreen extends JFrame implements DrawingTarget {

	private int width;
	private int height;

	CenterPanel center;
	CountdownClock cc;

	public BigScreen(int width, int height) {
		this.width = width;
		this.height = height;
		

		Container c = getContentPane();
		c.setLayout(null);

		c.setBackground(Color.white);
		//setBackground();

		addCountDownClock(c);

		addCenterPanel(c);

		new Sequencer(this);

	}
	
	/*private void setBackground() {
		try {
			Container c = getContentPane();
			
			JPanel panel = new JPanel();
			panel.set
			
			panel.add(new JLabel(new ImageIcon(ImageIO.read(new File("img/background/moon.jpg")))));

			c.add(panel);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}*/

	private void addCountDownClock(Container c) {
		cc = new CountdownClock();

		int clockHeight = cc.getPreferredSize().height;
		int clockWidth = cc.getPreferredSize().width;

		int x = width - (clockWidth + (width / 25));
		int y = height - (clockHeight + (height / 17));

		cc.setBounds(x, y, clockWidth, clockHeight);

		c.add(cc);

		// FadeOut.start(cc, 2500);
		//FadeIn.start(cc, 2500);
	}

	@Override
	public void setTitle(String title) {
		super.setTitle(title);

		center.setTitle(title);

	}

	@Override
	public void setTitle(String title, FxListener listener) {
		super.setTitle(title);

		center.setTitle(title, listener);
	}

	private void addCenterPanel(Container c) {
		center = new CenterPanel(width, 330);

		c.add(center);

		int panelHeight = center.getSize().height;
		int panelWidth = center.getSize().width;

		int y = (height - panelHeight) / 2;

		center.setBounds(0, y, panelWidth, panelHeight);
	}

	@Override
	public Container getCenterContainer() {
		return center.getContentPane();
	}

	@Override
	public Container getMainContainer() {
		return getContentPane();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();

		GraphicsDevice[] devices = graphicsEnvironment.getScreenDevices();
		
		DisplayMode dm = devices[1].getDisplayMode();
		final int h = dm.getHeight();
		final int w = dm.getWidth();
	
		
		SwingUtilities.invokeLater(new Runnable() {

			public void run() {
				BigScreen ex = new BigScreen(w, h);
				ex.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

				ex.setUndecorated(true);

				ex.setIgnoreRepaint(true);
				ex.setVisible(true);
				ex.createBufferStrategy(2);

				
				
				GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[1].setFullScreenWindow(ex);
			}
		});
		
		
	}

	@Override
	public void showClock(long start) {
		cc.setStart(start);
		
		FadeIn.start(cc, 2500);
		
	}

	@Override
	public void hideClock() {
		FadeOut.start(cc, 2500);

	}

}
